<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/7/15.
 */

namespace App\Service;

use App\Models\Ad;
use App\Service\Contract\AdInterface;
use Zane\View\Service\Transfer;

class AdService implements AdInterface
{
    public function read(Transfer $transfer): array
    {
        $model = Ad::query();
        if ($pid = $transfer->get('pid')) {
            if (\is_array($pid)) {
                $model->whereIn('ad_position_id', $pid);
            } else {
                $model->where('ad_position_id', (int) $pid);
            }
        }
        // TODO 多语言处理，需要验证
        if ($transfer->getLang()) {
            // $model->where('lang', $transfer->getLang());
        }
        if (!blank($transfer->getOrder())) {
            foreach ($transfer->getOrder() as $key => $val) {
                $model->orderBy($key, $val);
            }
        }

        return $model->limit($transfer->getLimit())->get()->toArray();
    }
}
